{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example Notebook for transport measurements "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## starting qkit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import time\n",
    "import qkit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qkit.cfg['run_id']='run_number'\n",
    "qkit.cfg['user']='User'\n",
    "qkit.start()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qkit.measure.transport import transport\n",
    "import qkit.measure.samples_class as sc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Initialize devices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Source measure unit combined with tunnel electronic as IV device"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "SMU = qkit.instruments.create('SMU', 'Keithley', address='TCPIP0::00.00.000.00::INSTR', reset=True)  # Keithley_2636A\n",
    "#SMU = qkit.instruments.create('SMU', 'Yokogawa', address='TCPIP0::00.00.000.00::INSTR', reset=True)  # Yokogawa_GS820\n",
    "IVD = qkit.instruments.create('IVD', 'virtual_tunnel_electronic', SMU=SMU)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "alternative: Source measure unit as IV device"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "IVD = qkit.instruments.create('IVD', 'Keithley', address='TCPIP0::00.00.000.00::INSTR', reset=True)  # Keithley 2636A\n",
    "#IVD = qkit.instruments.create('IVD', 'Yokogawa', address='TCPIP0::00.00.000.00::INSTR', reset=True)  # Yokogawa GS820"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Microwave source"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mw_src = qkit.instruments.create('mw_src','Anritsu_MG37022', address = 'TCPIP0::00.00.000.00::inst0::INSTR')  # Anritsu MG37022"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Temperature control"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tip = qkit.instruments.create('tip', 'TIP_client', address='IP address', port='PORT')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Initialize transport measurements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reload(transport)\n",
    "tr = transport.transport(IVD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample = sc.Sample()\n",
    "sample.name = 'A0000#0'\n",
    "sample.comment = 'This sample looks promising.'\n",
    "sample.ports = 'I0to0_V0to0'\n",
    "print(sample.get_all())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tr.set_filename(filename='filename')\n",
    "tr.set_expname(expname='expname')\n",
    "tr.set_comment(comment='comment')\n",
    "tr.set_sample(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "IVD.reset()\n",
    "IVD.set_defaults(sweep_mode=0)  # sweep_mode: 0 (VV-mode) | 1 (IV-mode) | 2 (VI-mode)\n",
    "IVD.get_all()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# measure 1D scan"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "IVD settings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "IVD.set_sense_nplc(1)  # number of power line cycles\n",
    "IVD.set_sense_average(1)  # average\n",
    "IVD.set_bias_delay(15e-6) # for Yokogawa GS820 it should be >= 200e-6 to ensure correct data storage\n",
    "IVD.set_sense_delay(15e-6)  # measurement delay"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "################\n",
    "# current bias #\n",
    "################\n",
    "IVD.set_sweep_mode(0) # 0 (VV-mode) | 1 (IV-mode) | 2 (VI-mode)\n",
    "IVD.set_pseudo_bias_mode(0) # 0 (current bias) | 1 (voltage bias)\n",
    "IVD.set_dAdV(2e-6)  # conversion factor of voltage biased current source (U -> I)\n",
    "IVD.set_amp(1e3)  # preamplifier factor\n",
    "IVD.set_BW(100)  # bandwidth of external filter (in Hz)\n",
    "IVD.set_tau_amp(100)  # time constant of external amplifier (τ=2πRC in s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "################\n",
    "# voltage bias #\n",
    "################\n",
    "IVD.set_pseudo_bias_mode(1)  # voltage bias instead of IVD.set_pseudo_bias_mode(0) \n",
    "IVD.set_Vdiv(1e3)  # external voltage divider factor (e.g. 1e3)\n",
    "IVD.set_dVdA(1e8)  # amplification factor for voltage bias (e.g. 1e9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "transport settings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tr.set_dVdI(True)  # data series of numerical gradient\n",
    "tr.set_average(3)  # average entire traces\n",
    "tr.sweeps.reset_sweeps()\n",
    "tr.add_sweep_4quadrants(start=0, stop=100e-9, step=1e-9, offset=0) # 4 quadrants sweep: start at zero -> stop -> zero -> -stop -> zero\n",
    "#tr.sweeps.add_sweep(start=0, stop=0, step=0) # single sweep"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "start 1D measurement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tr.measure_1D()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2D scan"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "IVD settings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "IVD.set_sense_nplc(1)  # number of power line cycles\n",
    "IVD.set_sense_average(1)  # average\n",
    "IVD.set_bias_delay(15e-6) # for Yokogawa GS820 it should be >= 200e-6 to ensure correct data storage\n",
    "IVD.set_sense_delay(15e-6)  # measurement delay"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "################\n",
    "# current bias #\n",
    "################\n",
    "IVD.set_sweep_mode(0) # 0 (VV-mode) | 1 (IV-mode) | 2 (VI-mode)\n",
    "IVD.set_pseudo_bias_mode(0) # 0 (current bias) | 1 (voltage bias)\n",
    "IVD.set_dAdV(2e-6)  # conversion factor of voltage biased current source (U -> I)\n",
    "IVD.set_amp(1e3)  # preamplifier factor\n",
    "IVD.set_BW(100)  # bandwidth of external filter (in Hz)\n",
    "IVD.set_tau_amp(100)  # time constant of external amplifier (τ=2πRC in s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "################\n",
    "# voltage bias #\n",
    "################\n",
    "IVD.set_pseudo_bias_mode(1)  # voltage bias instead of IVD.set_pseudo_bias_mode(0) \n",
    "IVD.set_Vdiv(1e3)  # external voltage divider factor (e.g. 1e3)\n",
    "IVD.set_dVdA(1e8)  # amplification factor for voltage bias (e.g. 1e9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "transport settings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tr.set_dVdI(True)  # data series of numerical gradient\n",
    "tr.set_average(2)  # average entire traces\n",
    "tr.sweeps.reset_sweeps()\n",
    "tr.add_sweep_4quadrants(start=0, stop=0, step=0, offset=0) # 4 quadrants sweep: start at zero -> stop -> zero -> -stop -> zero\n",
    "#tr.sweeps.add_sweep(start=0, stop=0, step=0) # single sweep"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2D scan settings (example: microwave power scan)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mw_src.set_frequency(0.)  # set frequency\n",
    "mw_src.set_status(True)\n",
    "\n",
    "# x function that is executed at each iteration\n",
    "def x_function(i):\n",
    "    mw_src.enable_high_power = False\n",
    "    mw_src.set_power(i)\n",
    "    time.sleep(0.01)\n",
    "    return\n",
    "\n",
    "tr.set_x_parameters(x_vec=numpy.arange(0,0,0), # (start,stop,step) \n",
    "                    x_coordname='power', \n",
    "                    x_set_obj=x_function, \n",
    "                    x_unit ='dBm')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "landscape scan (optional)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def lsc_func(x, y0, y1, x0):\n",
    "    y = np.ones(shape=len(x))*y1\n",
    "    y[np.abs(x) > x0] = y0\n",
    "    return y\n",
    "tr.set_landscape(lsc_func, (0, 0, 0))\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "fig, ax = plt.subplots()\n",
    "ax.set_xlabel('{:s} ({:s})'.format(tr._x_coordname, tr._x_unit))\n",
    "ax.set_ylabel('{:s} ({:s})'.format(tr._x_name, tr._x_unit))\n",
    "ax.plot(tr._x_vec, tr._lsc_vec)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "log function, e.g. temperature tracking (optional)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_T():\n",
    "    return tip.get_T()\n",
    "\n",
    "tr.set_log_function([get_T], ['temp'], ['K'], ['f'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "start 2D measurement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tr.measure_2D()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3D scan"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "IVD settings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "IVD.set_sense_nplc(1)  # number of power line cycles\n",
    "IVD.set_sense_average(1)  # average\n",
    "IVD.set_bias_delay(15e-6) # for Yokogawa GS820 it should be >= 200e-6 to ensure correct data storage\n",
    "IVD.set_sense_delay(15e-6)  # measurement delay"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "################\n",
    "# current bias #\n",
    "################\n",
    "IVD.set_sweep_mode(0) # 0 (VV-mode) | 1 (IV-mode) | 2 (VI-mode)\n",
    "IVD.set_pseudo_bias_mode(0) # 0 (current bias) | 1 (voltage bias)\n",
    "IVD.set_dAdV(2e-6)  # conversion factor of voltage biased current source (U -> I)\n",
    "IVD.set_amp(1e3)  # preamplifier factor\n",
    "IVD.set_BW(100)  # bandwidth of external filter (in Hz)\n",
    "IVD.set_tau_amp(100)  # time constant of external amplifier (τ=2πRC in s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "################\n",
    "# voltage bias #\n",
    "################\n",
    "IVD.set_pseudo_bias_mode(1)  # voltage bias instead of IVD.set_pseudo_bias_mode(0) \n",
    "IVD.set_Vdiv(1e3)  # external voltage divider factor (e.g. 1e3)\n",
    "IVD.set_dVdA(1e8)  # amplification factor for voltage bias (e.g. 1e9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "transport settings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tr.set_dVdI(True)  # data series of numerical gradient\n",
    "tr.set_average(2)  # average entire traces\n",
    "tr.sweeps.reset_sweeps()\n",
    "tr.add_sweep_4quadrants(start=0, stop=0, step=0, offset=0) # 4 quadrants sweep: start at zero -> stop -> zero -> -stop -> zero\n",
    "#tr.sweeps.add_sweep(start=0, stop=0, step=0) # single sweep"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3D scan settings (example: microwave power and frequency scan)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mw_src.set_status(True)\n",
    "\n",
    "'''mw power variation'''\n",
    "def x_function(i):\n",
    "    mw_src.enable_high_power = False\n",
    "    mw_src.set_power(i)\n",
    "    time.sleep(0.01)\n",
    "    return\n",
    "\n",
    "tr.set_x_parameters(x_vec=numpy.arange(0,0,0), # (start,stop,step) \n",
    "                    x_coordname='power', \n",
    "                    x_set_obj=x_function, \n",
    "                    x_unit ='dBm')\n",
    "\n",
    "'''mw frequency variation'''\n",
    "def y_function(i):\n",
    "    mw_src.set_status(True)\n",
    "    time.sleep(0.1)\n",
    "    mw_src.set_frequency(i)\n",
    "    return \n",
    "\n",
    "tr.set_y_parameters(y_vec=numpy.arange(0.0e9, 20.0e9, 1.0e9), #  (start, stop, step)        \n",
    "                    y_coordname='mw_frequency', \n",
    "                    y_set_obj=y_function, \n",
    "                    y_unit ='Hz')\n",
    "\n",
    "\n",
    "#log function, e.g. temperature tracking\n",
    "def get_T():\n",
    "    return tip.get_T()\n",
    "\n",
    "tr.set_log_function([get_T], ['temp'], ['K'], ['f'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "start 3D measurement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tr.measure_3D()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# measure trace"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "e.g. time trace of voltage and current"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if hasattr(tr, 'tmp'): del(tr.tmp)\n",
    "def get_time():\n",
    "    if not hasattr(tr, 'tmp'):\n",
    "        tr.tmp = time.time()\n",
    "    return time.time()-tr.tmp\n",
    "\n",
    "tr.set_xy_parameters(x_name='time',\n",
    "                     x_func=get_time,\n",
    "                     x_vec=np.arange(0, 10, 0.1),\n",
    "                     x_unit='s',\n",
    "                     y_name=['voltage', 'current'],\n",
    "                     y_func=[IVD.get_voltage, IVD.get_current],\n",
    "                     y_unit=['V', 'A'],\n",
    "                     x_dt=1e-3)\n",
    "\n",
    "tr.set_views(view=[(1,2)])  # add view I(V)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tr.measure_xy()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# stop qkit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "IVD.close()  # in particular necessary for Keithley 2636A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "exit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
